package com.lingo.ffmpegdemo.lg.iodemo;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author watson
 * @date 2021/6/8 14:52
 * @Description: 读取文件，根据内容，写到对应的文件
 **/
public class WriteByContent {

    public static void main(String argv[]) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\data-kafka\\kafka\\");
        String[] list = file.list();
        Map<String, String> map = new HashMap();
        for (String s : list) {
            getBuffer(s);
            System.out.println("succee");
        }

        System.out.println("succeed!");

    }

    public static void getBuffer(String fileName) {
        try {
            String fileNames = "C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\data-kafka\\kafka\\" + fileName;
//            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\hk_kafka-data\\" + fileName));
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileNames), "GBK"));
            String s = null;
            int a = 0;
            BufferedWriter bw = null;
            while ((s = br.readLine()) != null) {
                //右线6号青岛入口
                if (s.contains("0deff95a0d0942508af77a8b77568454")) {
                    String name = "右线6号青岛入口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //右线9号青岛入口(台三)
                if (s.contains("014eb55985ba4a5cb8ce6bbfb17856f6")) {
                    String name = "右线9号台三入口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //右线10号黄岛出口
                if (s.contains("c9e612c4562c48d0938c28e5863398d8")) {
                    String name = "右线10号黄岛出口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //右线断面
                if (s.contains("d8f3303a9cc645389d977897fb5a94c9")) {
                    String name = "右线7号断面";
                    bw = getBufferedWriter(s, name, bw);
                }
                //右线海底
                if (s.contains("af8dc3626cfd4dd1a3b3dece8e29cb0f")) {
                    String name = "右线8号海底";
                    bw = getBufferedWriter(s, name, bw);
                }


                //左线1号黄岛入口
                if (s.contains("5203f5da7a1f44318d8372e743b27eb0")) {
                    String name = "左线1号黄岛入口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //左线4号青岛出口(团岛)
                if (s.contains("c00437b9030c4487b98ac0d1b09ff34b")) {
                    String name = "左线4号团岛出口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //左线5号青岛出口
                if (s.contains("20271af08fd24298a4cdc4845a29ed71")) {
                    String name = "左线5号青岛出口";
                    bw = getBufferedWriter(s, name, bw);
                }
                //左线断面
                if (s.contains("819af0cf44bc4b0186204e6735947554")) {
                    String name = "左线3号断面";
                    bw = getBufferedWriter(s, name, bw);
                }
                //左线海底
                if (s.contains("41d5a9b92b714176b754276e8dccaf06")) {
                    String name = "左线2号海底";
                    bw = getBufferedWriter(s, name, bw);
                }
            }

            bw.flush();
            if (bw != null) {
                bw.close();
            }
            br.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static BufferedWriter getBufferedWriter(String s, String a, BufferedWriter bw) throws IOException {
        if (bw != null) {
            //首先判断前一个文件是否为空，如不为空，可能在写前一个文件，则将其关闭。
            bw.close();
            //新建文件
            bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + a + ".txt", true));
        } else { //若为空，则是第一个文件
            bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + a + ".txt", true));
        }
        //将BC这一行内容写入
        bw.write(s + "\n");
        return bw;
    }
}
